#include "CompressForm.h"
#include "ui_CompressForm.h"
#include <QFileInfo>
#include<QDebug>
#include <QDateTime>
#include"thread"
#include "PlayerWindow/AppConfig.h"
#include <QFileDialog>
#include "PlayerWindow/Base/FunctionTransfer.h"
#include <QMessageBox>
CompressForm::CompressForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CompressForm)
{
    ui->setupUi(this);
     FunctionTransfer::init(QThread::currentThreadId());

    // 去掉问号，只保留关闭
    setWindowTitle("压缩视频");
    setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
    resize(500,550);
    //将窗口永远置于上层可见 不会被遮盖
//    setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
    //阻塞除当前窗口之外的所有窗口
    //setWindowModality(Qt::ApplicationModal);

    pd = new QProgressDialog("当前进度...", "取消", 0, 100);
    pd->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);        //不显示进度条上的“最小化”“最大化”“关闭”
    pd->setWindowTitle("视频压缩中......");    //窗口标题
    pd->setAutoClose(true);        //进度达到最大值时不关闭，默认为true
    pd->setLabelText("视频压缩中...");    //显示的文本
    pd->setRange(0, 100);                //设置进度条的极值，默认为[0,100]
    pd->reset();
    //pd->setCancelButton(NULL);            //不显示取消按钮
    pd->setModal(true);
    //    pd->show();
    pd->hide();
}

CompressForm::~CompressForm()
{
    delete ui;
    delete pd;
}
void CompressForm::init(int64_t rate, int fps, int width, int height,QString fileName){
    filePath = fileName;
    qDebug()<<"比特率："<<QString::number(rate);
    ui->label_2->setText(QString::number(rate).append(" kb/s"));
   ui->label_4->setText(QString::number(fps).append(" fps"));
   ui->label_6->setText(QString::number(width));
    ui->label_8->setText(QString::number(height));
    ui->label_10->setText(fileName);
    ui->label_10->adjustSize();
    ui->checkBox->setChecked(true);
    ui->checkBox_2->setChecked(true);
    ui->lineEdit->setText(QString::number(width));
      ui->lineEdit_2->setText(QString::number(height));
      ui->horizontalSlider_2->setRange(0,51);
       ui->horizontalSlider_2->setPageStep(1);
      ui->horizontalSlider->setRange(0,rate);
      ui->horizontalSlider_2->setValue(fps);
      ui->horizontalSlider->setValue(rate);

      ui->horizontalSlider->setPageStep(100);
      ui->lineEdit_3->setText(AppConfig::videoFileOutPath);
      QString s = "/";
      QString s1 ="/ 51";
      ui->label_18->setText(s.append(QString::number(rate)));
      ui->label_19->setText(s1);




}
void CompressForm::on_horizontalSlider_valueChanged(int value)
{
    ui->label_20->setText(QString::number(value));
}

void CompressForm::on_horizontalSlider_2_valueChanged(int value)
{
    ui->label_21->setText(QString::number(value));
}
void CompressForm::on_pushButton_clicked(){
    QString s = QFileDialog::getExistingDirectory(
               this, QStringLiteral("选择要存放的路径"),
                AppConfig::videoFileOutPath//初始目录
                  );
    if (!s.isEmpty())
    {

        qDebug()<<s;

        std::string path=s.toStdString();
        ui->lineEdit_3->setText(s);
        AppConfig::videoFileOutPath = s;
        AppConfig::saveConfigInfoToFile();
    }
}
void CompressForm::on_pushButton_2_clicked()
{
    QString width = ui->lineEdit->text();
    QString height = ui->lineEdit_2->text();
     int rate = ui->horizontalSlider->value();
     int fps = ui->horizontalSlider_2->value();
    bool audio= ui->checkBox->checkState()==Qt::Checked?true:false;
    bool video= ui->checkBox_2->checkState()==Qt::Checked?true:false;
    QFileInfo fileInfo(filePath);
    QString fileName = fileInfo.fileName();
   QString currentDate= QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
    QString dstPath = ui->lineEdit_3->text().append("/out_").append(currentDate).append(fileName);
    qDebug()<<dstPath;
     Transcode transcode(filePath.toStdString(),dstPath.toStdString(),width.toInt(),height.toInt(),rate*1000,fps,video,audio);
     transcode.setVideoCompressCallBack(this);
///创建一个线程专门用来解码视频
    pd->show();
    std::thread([&](Transcode transcode)
    {
        transcode.doTranscode();
    }, transcode).detach();

}
void CompressForm::changeCompressProcess(const double &process){
    FunctionTransfer::runInMainThread([=]()
    {

        int steps = process*100;
            if(steps>pd->value()){
                 qDebug()<<process<<steps<<pd->value();
                pd->setValue(steps);
                if(steps==100){
                     pd->setValue(0);
                     pd->close();
                  int k=  QMessageBox::information(this, "成功", QString("压缩成功！"),"确定","关闭",0,0);
                  if(k==0){

                  }else if(k==1){
                       qDebug()<<"关闭";
                       this->hide();
                  }
                }
            }
    });

}



